package com.iqiyi.pps.epg.core.service.statistic;

import com.alibaba.fastjson.JSONObject;
import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.statistic.TStatisticAutoNewData;
import com.iqiyi.pps.epg.api.model.web.statistic.TStatisticAutoNewDataPage;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.dao.statistic.StatisticAutoNewDataDao;
import com.iqiyi.pps.epg.core.model.statistic.StatisticAutoNewData;
import com.iqiyi.pps.epg.core.utils.TransUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by zhang on 2016/1/25.
 */
@Service
@Transactional
public class StatisticAutoNewDataServiceImpl implements StatisticAutoNewDataService {
    protected Logger logger = LoggerFactory.getLogger(StatisticAutoNewDataServiceImpl.class);
    @Autowired
    private StatisticAutoNewDataDao dao;

    @Override
    public long save(StatisticAutoNewData entity) {
        dao.saveOnly(entity);
        return entity.getId();
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public TStatisticAutoNewDataPage findTStatisticAutoNewDataPage(TPage page, TFilter filter) {
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        Page<StatisticAutoNewData> dbPage = TransUtil.buildPage(page);
        Page<StatisticAutoNewData> pageData = dao.findPage(dbPage, filterList);
        List<StatisticAutoNewData> dataList = pageData.getResult();
        TStatisticAutoNewDataPage carouselPage = new TStatisticAutoNewDataPage();
        page.setTotalCount(pageData.getTotalCount());
        carouselPage.setTPage(page);
        List<TStatisticAutoNewData> resultList = dao.transToThriftList(dataList);
        carouselPage.setResult(resultList);
        return carouselPage;
    }

    //count(id) ,sum( IF( percent = 100 , 1,0 ) ) sumNum
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public JSONObject getAutoOtherDataRate(TFilter filter) {
        JSONObject jsonObject = new JSONObject();
        // 平均准确率1
        String hql = "SELECT avg(percent) from StatisticAutoNewData where autoTitleCharLen > 0 ";
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        hql = TransUtil.buildQueryStringByPropertyFilter(hql, filterList);
        Object result1 = dao.createQuery(hql).uniqueResult();
        jsonObject.put("avg", result1 == null ? "0" : result1 + "");
        // 平均准确率2 去除 charlen > 43
        hql = "SELECT avg(percent) from StatisticAutoNewData where  autoTitleCharLen > 0 and autoTitleCharLen < 44";
        filterList = TransUtil.builderFilterList(filter);
        hql = TransUtil.buildQueryStringByPropertyFilter(hql, filterList);
        Object result2 = dao.createQuery(hql).uniqueResult();
        jsonObject.put("avg2", result2 == null ? "0" : result2 + "");
        // sum(case bd.domainType when 1 then bd.domainQry else 0 end) 替代 if
        hql = "SELECT count(id),sum( autoFlag ),sum( titleError ) ," +
                "sum( case when percent = 100 and autoTitleCharLen < 44 and autoTitleCharLen > 0  then 1 else 0 end  ) ," +
                "sum( case when autoTitleCharLen < 44 and autoTitleCharLen > 0 then 1 else 0 end  ), " +
                "sum( case when percent = 100 then 1 else 0 end  )," +
                "sum( case when autoTitleCharLen > 0 then 1 else 0 end  ) " +
                " from StatisticAutoNewData ";
        filterList = TransUtil.builderFilterList(filter);
        hql = TransUtil.buildQueryStringByPropertyFilter(hql, filterList);
        List result = dao.createQuery(hql).list();
        Object[] obj = (Object[]) result.get(0);
        logger.info("count {} , sum autoFlag{},sum titleError{} ,{}|{}|{}|{}", obj[0], obj[1], obj[2], obj[3], obj[4], obj[5], obj[6]);

        jsonObject.put("used", "0");
        jsonObject.put("titleError", "0");
        jsonObject.put("fullPercent", "0");
        jsonObject.put("fullPercent2", "0");
        if (Integer.valueOf(obj[0] + "") == 0) {
            return jsonObject;
        }
        int total = Integer.valueOf(obj[0] + "");
        int zeroCount = Integer.valueOf(obj[6] + "");

        double rate1 = Integer.valueOf(obj[1] + "") * 100.0 / total;
        double rate2 = Integer.valueOf(obj[2] + "") * 100.0 / total;

        double rate3 = 0;
        double rate4 = 0;
        if ( Integer.valueOf(obj[6] + "") != 0) {
            rate3 = Integer.valueOf(obj[5] + "") * 100.0 / Integer.valueOf(obj[6] + "");
        }
        //去除 charlen > 43
        if (Integer.valueOf(obj[4] + "") != 0) {
            rate4 = Integer.valueOf(obj[3] + "") * 100.0 / Integer.valueOf(obj[4] + "");
        }

        jsonObject.put("used", "" + rate1);
        jsonObject.put("titleError", "" + rate2);
        jsonObject.put("fullPercent", "" + rate3);
        jsonObject.put("fullPercent2", "" + rate4);//去除 charlen > 43
        return jsonObject;
    }
}
